import numpy as np
from tensorflow.python.keras.models import load_model
from sklearn.preprocessing import StandardScaler
import joblib
import os
import matplotlib.pyplot as plt


def predict(model_path, name):
    path = os.getcwd()

    thruster_normal = np.load('./normalize_data/predict/predict_x.npy')  # 上一步标准化处理之后的常温数据
    # lstm_model = load_model("./model/thruster_model.h5")  # 调用的具体模型
    lstm_model = load_model(model_path)
    normal = []
    for i in range(1, thruster_normal.shape[2]):
        normal.append(thruster_normal[:, :, [0, i]])

    y_predict = lstm_model(normal)

    for i in range(1, thruster_normal.shape[2]):
        exec("scalery%s=joblib.load('./normalize_data/train/scalery%s.pkl')" % (i, i))
        exec(
            "y%s=scalery%s.inverse_transform(np.array(y_predict[i-1])).reshape(np.array(y_predict[i-1]).shape[0],np.array(y_predict[i-1]).shape[1],1)" % (
                i, i))
        try:
            exec("temp = np.concatenate((temp,y%s),axis=2)" % i)
        except:
            exec("temp = y%s" % i)

        plt.figure(figsize=(20, 16), dpi=100)
        ax = plt.subplot(111)
        ax.yaxis.get_offset_text().set_fontsize(40)
        exec('plt.plot(y%s[0:1, :, :].reshape(-1), linewidth=5)' % i)
        plt.tick_params(labelsize=40)
        plt.title(u'预测结果', fontproperties='SimHei', fontsize=40)
        plt.xlabel(u'时间点', fontproperties='SimHei', fontsize=40)
        plt.ylabel(name[i - 1], fontproperties='SimHei', fontsize=40)
        plt.savefig('./img/predict_res' + str(i) + '.png')

    np.save("./predict_result/thruster_outlier_data.npy", locals()["temp"])


if __name__ == '__main__':
    predict("./model/thruster_model.h5")
